package com.lovepick;

import java.util.List;

import org.apache.http.cookie.Cookie;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.lovepick.bean.Order;
import com.lovepick.bean.User;
import com.lovepick.database.DatabaseHelper;
import com.lovepick.http.GetOrderService;
import com.lovepick.util.Constant;

public class GlobalApp extends Application
{
	public static final String COOKIE_MEMBER_ID = "memberID";
	public static final String COOKIE_MEMBER_PASSWORD = "memberPassword";
	public static final String PREFS_ROLE = "role";
	public static final String PREFS_ORDER_CURRENT = "order_current";
	public static final String PREFS_ORDER_RECMD = "order_recmd";

	private final static String TAG = "GlobalApp";
	private static GlobalApp mGlobalApp = null;
	private BMapManager mapManager = null;
	private int numMsgNew = 0;
	private final String key = "368781BA0403027777DA682D63972CE066162A44";

	@Override
	public void onCreate()
	{
		Log.v(TAG, "-----onCreate-----");
		mGlobalApp = this;
		getMapManager();

		if (getCookie() != null)
		{
			Log.i(TAG, mGlobalApp.getCookie());
		}
		else
		{
			Log.i(TAG, "mGlobalApp.getCookie() null...");
		}

		super.onCreate();
	}

	@Override
	public void onTerminate()
	{
		if (mapManager != null)
		{
			mapManager.destroy();
			mapManager = null;
		}
		super.onTerminate();
	}

	public boolean logout()
	{
		Log.i(TAG, "-----logout-----");
		deleteDatabase(DatabaseHelper.DB_NAME);
		SharedPreferences prefs = getSharedPreferences(Constant.PREFS, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		if (editor != null)
		{
			editor.clear();
			editor.commit();
			return true;
		}
		else
		{
			return false;
		}
	}

	public void saveRole(String role)
	{
		Log.i(TAG, "-----saveRole-----");
		GetOrderService.mRole = role;
		SharedPreferences p = getSharedPreferences(Constant.PREFS, Context.MODE_PRIVATE);
		SharedPreferences.Editor e = p.edit();
		e.putString(PREFS_ROLE, role);
		e.commit();
	}

	public String getRole()
	{
		Log.i(TAG, "-----getRole-----");
		SharedPreferences p = getSharedPreferences(Constant.PREFS, Context.MODE_PRIVATE);
		if (p != null)
		{
			if (p.contains(PREFS_ROLE))
			{
				return p.getString(PREFS_ROLE, Constant.PASSENGER);
			}
		}

		return null;
	}

	public final String getNickName()
	{
		User user = getLoginInfo();
		if (user != null)
		{
			return user.getNickName();
		}
		else {
			return Constant.EMPTY_STRING;
		}
	}

	public void setLoginInfo(User user)
	{
		Log.i(TAG, "-----setLoginInfo-----");
		SharedPreferences loginPrefs = mGlobalApp.getSharedPreferences(Constant.PREFS, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = loginPrefs.edit();
		editor.putString(User.USER_ID, user.getUserID());
		editor.putString(User.NICKNAME, user.getNickName());
		editor.putString(User.PASSWORD, user.getPassWord());
		editor.putString(User.AVATAR, user.getAvatar());
		editor.commit();
	}

	public User getLoginInfo()
	{
		Log.i(TAG, "-----getLoginInfo-----");
		SharedPreferences loginPrefs = mGlobalApp.getSharedPreferences(
				Constant.PREFS, Context.MODE_PRIVATE);
		User user = null;
		if (loginPrefs != null)
		{
			if (loginPrefs.contains(User.NICKNAME) && loginPrefs.contains(User.PASSWORD))
			{
				user = new User();
				user.setNickName(loginPrefs.getString(User.NICKNAME, null));
				user.setUserID(loginPrefs.getString(User.USER_ID, null));
				user.setAvatar(loginPrefs.getString(User.AVATAR, null));
				user.setPassWord(loginPrefs.getString(User.PASSWORD, null));

			}
		}

		return user;
	}

	public void setLastID(int lastID)
	{
		SharedPreferences loginPrefs = mGlobalApp.getSharedPreferences(
				Constant.PREFS, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = loginPrefs.edit();
		editor.putInt(User.LAST_ID, lastID);
		editor.commit();
	}

	public int getLastID()
	{
		int lastID = 0;
		SharedPreferences loginPrefs = mGlobalApp.getSharedPreferences(
				Constant.PREFS, Context.MODE_PRIVATE);
		if (loginPrefs != null)
		{
			if (loginPrefs.contains(User.LAST_ID))
			{
				lastID = loginPrefs.getInt(User.LAST_ID, 0);
			}
		}

		return lastID;
	}

	public String getCookie()
	{
		String cookie = null;
		SharedPreferences cookiePrefs = mGlobalApp.getSharedPreferences(
				Constant.PREFS, Context.MODE_PRIVATE);
		if (cookiePrefs != null)
		{
			if (cookiePrefs.contains(COOKIE_MEMBER_ID)
					&& cookiePrefs.contains(COOKIE_MEMBER_PASSWORD))
			{
				cookie = COOKIE_MEMBER_ID + "=";
				cookie += cookiePrefs.getString(COOKIE_MEMBER_ID, null);
				cookie += ";" + COOKIE_MEMBER_PASSWORD  + "=";
				cookie += cookiePrefs.getString(COOKIE_MEMBER_PASSWORD, null);
			}
		}
		return cookie;
	}

	public void setCookie(List<Cookie> cookies)
	{
		SharedPreferences cookiePrefs = mGlobalApp.getSharedPreferences(
				Constant.PREFS, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = cookiePrefs.edit();
		if (!cookies.isEmpty())
		{
			for (Cookie cookie : cookies)
			{
				editor.putString(cookie.getName(), cookie.getValue());
			}
			editor.commit();
		}
	}

	private void _saveOrder(String prefsName, Order order)
	{
		Log.i(TAG, "-----保存订单-----");
		SharedPreferences prefs = getSharedPreferences(prefsName, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt(Order.ORDER_ID, order.getOrderID());
		editor.putString(Order.NICKNAME, order.getNickName());
		editor.putString(Order.SRC, order.getSrc());
		editor.putString(Order.SLAT, String.valueOf(order.getSlat()));
		editor.putString(Order.SLNG, String.valueOf(order.getSlng()));
		editor.putString(Order.DEST, order.getDest());
		editor.putString(Order.DLAT, String.valueOf(order.getDlat()));
		editor.putString(Order.DLNG, String.valueOf(order.getDlng()));
		editor.putString(Order.GOTIME, order.getGoTime());
		editor.putInt(Order.OFFSET, order.getOffset());
		editor.putInt(Order.COUNT, order.getCount());
		editor.putInt(Order.MONEY, order.getMoney());
		editor.putString(Order.STATE, order.getState());

		editor.commit();
	}

	public void saveCurrentOrder(Order order)
	{
		Log.i(TAG, "-----保存当前订单-----");
		_saveOrder(PREFS_ORDER_CURRENT, order);
	}

	private Order _getOrder(String prefsName)
	{
		Log.i(TAG, "-----取出订单-----");
		SharedPreferences prefs = getSharedPreferences(prefsName, Context.MODE_PRIVATE);
		Order order = null;
		if (prefs != null)
		{
			if (prefs.contains(Order.SRC) && prefs.contains(Order.DEST))
			{
				order = new Order();
				order.setOrderID(prefs.getInt(Order.ORDER_ID, 0));
				order.setNickName(prefs.getString(Order.NICKNAME, null));
				order.setSrc(prefs.getString(Order.SRC, null));
				order.setSlat(Double.parseDouble(prefs.getString(Order.SLAT, "0")));
				order.setSlng(Double.parseDouble(prefs.getString(Order.SLNG, "0")));
				order.setDest(prefs.getString(Order.DEST, null));
				order.setDlat(Double.parseDouble(prefs.getString(Order.DLAT, "0")));
				order.setDlng(Double.parseDouble(prefs.getString(Order.DLNG, "0")));
				order.setCount(prefs.getInt(Order.COUNT, 0));
				order.setGoTime(prefs.getString(Order.GOTIME, ""));
				order.setOffset(prefs.getInt(Order.OFFSET, 0));
				order.setMoney(prefs.getInt(Order.MONEY, 0));
				order.setState(prefs.getString(Order.STATE, null));
			}
		}

		return order;
	}

	/**
	 * 用户在发布订单页面成功发布订单后, 该订单信息会在本地存储;<br>
	 * 此函数即是取出该订单信息, 以在相应位置显示.
	 *
	 * @return	保存的订单信息
	 */
	public Order getCurrentOrder()
	{
		Log.i(TAG, "-----取出当前订单-----");
		return _getOrder(PREFS_ORDER_CURRENT);
	}

	public void saveRecmdOrder(Order order)
	{
		Log.i(TAG, "-----保存当前选中的推荐订单-----");
		_saveOrder(PREFS_ORDER_RECMD, order);
	}

	/**
	 * 在地图页面, 乘客用户选择推荐的某项司机订单后, 与司机聊天
	 * 在离开地图页面之前, 保存选中的推荐订单, 在聊天要用到.
	 *
	 * @return	保存的订单信息
	 */
	public Order getRecmdOrder()
	{
		Log.i(TAG, "-----取出当前选中的推荐订单-----");
		return _getOrder(PREFS_ORDER_RECMD);
	}

	public static GlobalApp getInstance()
	{
		return mGlobalApp;
	}

	public BMapManager getMapManager()
	{
		if (null == mapManager)
		{
			mapManager = new BMapManager(this);
			mapManager.init(key, new MyGeneraListener());
			mapManager.getLocationManager().setNotifyInternal(10, 5);
		}
		return mapManager;
	}

	/**
	 * 获取百度地图授权码
	 *
	 * @return 百度地图授权码
	 */
	public String getKey()
	{
		return key;
	}

	/**
	 * @return the numMsgNew
	 */
	public int getNumMsgNew()
	{
		return numMsgNew;
	}

	/**
	 * @param numMsgNew the numMsgNew to set
	 */
	public void setNumMsgNew(int numMsgNew)
	{
		this.numMsgNew = numMsgNew;
	}

	public static class MyGeneraListener implements MKGeneralListener
	{

		@Override
		public void onGetNetworkState(int error)
		{
			// TODO Auto-generated method stub
			Log.e(TAG,
					"MyGeneralListener:onGetNetworkState error is " + error);
			//BaseUtils.showToast(mGlobalApp.getApplicationContext(),
					//R.string.network_error);
		}

		@Override
		public void onGetPermissionState(int error)
		{
			// TODO Auto-generated method stub

		}

	}

}
